//
//  RadioDetailViewController.m
//  Leisure
//
//  Created by Alex on 16/7/5.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "RadioDetailViewController.h"

@interface RadioDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation RadioDetailViewController

- (NSMutableArray *)dataSource {
    
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //头视图
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    UIImageView *headerView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3)];
    headerView.backgroundColor = [UIColor brownColor];
       [RequestManager fecthDataWithUrl:@"http://api2.pianke.me/ting/radio_detail" parameter:@{@"radioid":self.radioID} mehtod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigdic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        [headerView sd_setImageWithURL:[NSURL URLWithString:bigdic[@"data"][@"radioInfo"][@"coverimg"]]];
        for (NSDictionary *smalldic in bigdic[@"data"][@"list"]) {
            
            RadioDetailModel *model = [[RadioDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:smalldic];
            [self.dataSource addObject:model];
        }
        NSLog(@"%@",bigdic);
    } updateUI:^{
        
        [self.tableView addSubview:headerView];
        [self.tableView reloadData];

        
    }];
    
}

#pragma mark  --------------TableViewDelegate----------------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text = [self.dataSource[indexPath.row] title];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 150.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RadioPlayViewController *radioPlay = [RadioPlayViewController shardManager];
    
    radioPlay.index = indexPath.row;
    
    radioPlay.dataSource = self.dataSource;
    
    [self.navigationController pushViewController:radioPlay animated:YES];
    
}


//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
